// miniprogram/pages/inspectiontask/cx/cx.js
var utils = require('../../../utils/getTool');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainData: {
      id: 0
    },
    active: 0,
    activeNames: ['0'],
    solverNameList:[],
    inspectorDetailList:[],
    inspectorDetailName: ""
  },
  btnSover() {
    var sdat = this.data.solverNameList;
    var uid = "";
    var inspectorDetailName = "";
    for (var i in sdat) {
      this.data.inspectorDetailName += sdat[i].solverName +" ";
      uid += sdat[i].userId + " ";
    }
    if (this.data.inspectorDetailName === "" || this.data.inspectorDetailName === ''){
      wx.showToast({
        title: '请选择巡检员',
        icon:"none"
      })
      return 
    }
    utils.req({ //修改传递过去的名称以及 任务id
      url: getApp().appData.site+"/accp/psInspectorDetail/rmOrUpdate",
      data:{
        id: this.data.taskId, //巡检任务编号
        inspectorDetailName: this.data.inspectorDetailName,
        userId: uid,
        createName:this.data.userInfo.userName
      },
      success:res=>{
        if (res.statusCode===200){
          wx.showToast({
            title: '操作成功 请重新查询!',
            icon:"none"
          })

        }
      }
    })
    this.setData({
      show: false
    })
  },
  remLeftView() {
    var sList = this.data.solverNameList;
    var sList2 = this.data.solverNameList;
    for (var i in sList2) {
      if (sList2[i].sid === "R") {
        sList.splice(i, 1)
      }
    }
    this.setData({
      solverNameList: sList
    })
  },
  addLeftView() {
    var iList = this.data.getAllInctor;
    //只要你点击选择那么 删除之前所有沉于数据
    this.data.solverNameList = [];
    var sList = this.data.solverNameList;
    console.log(this.data.mainData.id)
    for (var i in iList) {
      console.log(iList[i])
      if (iList[i].sid === this.data.mainData.id) {
        sList.push({
          solverName:iList[i].userName,
          userId: iList[i].id,
          sid:""
        });
      }
    }
    this.setData({
      solverNameList: sList
    })
  },
  remSover(e) { //右边删除
    var index = e.target.dataset.index
    this.data.solverNameList[index].sid = 'R'
    console.log(this.data.solverNameList[index])
    this.setData({
      solverNameList: this.data.solverNameList
    })
  },
  remSoverLeft(e) {
    var index = e.target.dataset.index
    this.data.solverNameList[index].sid = this.data.mainData.id
    console.log(this.data.solverNameList[index])
    this.setData({
      solverNameList: this.data.solverNameList
    })
  },
  seleSover(e) {//左边添加
    var index = e.target.dataset.index
    this.data.getAllInctor[index].sid = this.data.mainData.id
    this.setData({
      getAllInctor: this.data.getAllInctor
    })
  },
  selerem(e) {
    var index = e.target.dataset.index
    this.data.getAllInctor[index].sid = ""
    this.setData({
      getAllInctor: this.data.getAllInctor
    })
  },



  modMain(e){
    wx.navigateTo({
      url: '../addInsMain/addInsMain?index='+e.target.dataset.index
    })
  },
  tapquxiao(e){  //取消任务
    if (this.data.inspectionList[e.target.dataset.index].isCancelStr === "是") {
      wx.showToast({
        title: '任务已经取消了！',
        icon:"none"
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '是否取消该任务？',
      success: (res) => {
        if(res.confirm){
          utils.req({ //刷新对应服务器数据 以及本地显示出来的数据
            url:  getApp().appData.site+"/accp/psInspectiontaskMain/modIsCancel",
            data: {
              id: e.target.dataset.id,
              isCancel: e.target.dataset.isCancel
            },
            success: res => {
              if (res.data) {//取消成功就局部刷新这个数据 但是数据库已经刷新了
                wx.showToast({
                  title: '取消任务成功!',
                  icon: "none"
                })
                this.data.inspectionList[e.target.dataset.index].isCancelStr = "是";
                this.data.inspectionList[e.target.dataset.index].isCancel =  1 ;
                this.setData({
                  inspectionList: this.data.inspectionList
                })
              }
            }
          })
        }
      }
    })
  },
  tablook(e){
    wx.navigateTo({
      url: '../quetask/quetask?index=' + e.target.dataset.index,
    })
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      inspectionList: getApp().appData.inspectionList,//得到全局变量的值
      userInfo: wx.getStorageSync('userInfo')
    })
    console.log(this.data.inspectionList)
    utils.req({
      url:  getApp().appData.site+"/accp/psUser/fetchCondition",
      data: {
        col:"roleId",
        value:"3"
      },
      success: res => {
        console.log(res)
        this.setData({
          getAllInctor: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  showInspector(e) {
    /**
     * 根据你e得到选中的索引
     * 根据索引得到你的id 
     * 根据你的id得到该任务所有巡检员
     */
    console.log(e)
    utils.req({
      url: getApp().appData.site+"/accp/psInspectorDetail/fetchDetail",
      data:{
        column:"TASKID",
        val: this.data.inspectionList[e.target.dataset.index].id
      },
      success: res => {
        console.log(res)
        if (res.statusCode === 200) {
          this.setData({
            taskId: this.data.inspectionList[e.target.dataset.index].id,
            solverNameList:res.data,
            mainData:this.data.mainData 
          })
        }
      }
    })
    this.setData({
      show: true
    })
  },
  onClose(){
    this.setData({
      show: false
    })
  }
})